package com.yamaha.bikedesalon;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.yamaha.bikedesalon.util.API;
import com.yamaha.bikedesalon.util.SCStaticFunc;

public class CommentFragment extends AMyActivity {

	public CommentFragment() {
		super(MenuType.NONE);
	}

	private boolean isCreated = false;
	private JSONObject item;
	private final String TAG = getClass().getSimpleName();

	@Override
	protected void onStart() {
		super.onStart();

		final EditText cText = (EditText) findViewById(R.id.c_text);
		showInputText(cText);
	}

	@Override
	protected List<EditText> getEditTexts() {

		final ArrayList<EditText> editTexts = new ArrayList<EditText>();
		if (isCreated) {
			final EditText cText = (EditText) findViewById(R.id.c_text);
			editTexts.add(cText);
		}

		return editTexts;
	}

	@Override
	protected void onResume() {
		super.onResume();

		final EditText cText = (EditText) findViewById(R.id.c_text);
		showInputText(cText);
	}

	private void doComment() {
		Utils.showProgressDialog(this, "", "Commenting...");

		final AsyncHttpClient client = new AsyncHttpClient();

		final RequestParams params = new RequestParams();

		final EditText cText = (EditText) findViewById(R.id.c_text);

		try {
			params.put("user_id", SCStaticFunc.getUserId(this));
			params.put("item_id", item.getString("post_id"));
			// params.put("sub_type", item.getString("type"));
			params.put("comment", cText.getText().toString());

			client.post(API.COMMENT, params, new MyAsyncHttpResponseHandler(API.COMMENT, this) {

				@Override
				protected void onAsyncSuccess(final Object res)
						throws JSONException {
					Toast.makeText(CommentFragment.this, "Success", Toast.LENGTH_SHORT).show();
					finish();
				}

				@Override
				public void onFinish() {
					super.onFinish();
					Utils.closeAllDialog();
				}
			});
		} catch (final JSONException e) {
			LogUtil.e(TAG, "Error : " + e.getMessage(), e);
		}
	}

	@Override
	public void onMyClick(View v) {
		hideInputText();
		switch (v.getId()) {
			case R.id.c_done : {
				doComment();
			}
				break;
			case R.id.c_cancel : {
				finish();
			}
				break;
		}
	}

	@Override
	protected int getLayoutId() {
		return R.layout.comment_layout;
	}

	@Override
	protected void onMyCreated(Bundle savedInstanceState) {
		final EditText cText = (EditText) findViewById(R.id.c_text);
		cText.setText("");

		final String detail = getIntent().getStringExtra("detail");

		try {
			item = new JSONObject(detail);
		} catch (final JSONException e) {
			LogUtil.e(TAG, "Errror : " + e.getMessage(), e);
			finish();
			return;
		}

		final Button cancelButton = (Button) findViewById(R.id.c_cancel);
		final Button doneButton = (Button) findViewById(R.id.c_done);
		cancelButton.setOnClickListener(this);
		doneButton.setOnClickListener(this);

		isCreated = true;
	}
}
